﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Proggy.Web.Cms.ContentModel;
using Proggy.Web.Cms.Core.PropertyTypes;
using Proggy.Web.Cms.Core.Configuration.ConfigObjects;
using Newtonsoft.Json;
using Proggy.Web.Cms.Repositories.ContentModel;
using Proggy.Web.Cms.Repositories.Published;
using Proggy.Web.Cms.Repositories.Membership;

namespace Proggy.Web.Cms.Core.ContentModels
{
    /// <summary>
    /// For non-viewable pages (ie data or data containers) - most likely to be used to inherit from
    /// </summary>
    [ContentModel("Container redirect", "~/Areas/Proggy/Content/Images/ContentModelIcons/fast-forward.png", false, false, "", "N/a", "Proggy.Web.Cms.Core.Controllers.ContentModel")]
    public class ProggyContainerRedirectContentModel : ProggyCoreContentModelBase, IContentModel
    {
        public ProggyContainerRedirectContentModel(IContentModelTypeRepository cmRepo, IMembershipRepository membRepo) : base(cmRepo,  membRepo) { }
        public ProggyContainerRedirectContentModel() : base() { }

        /// <summary>
        /// If false - throws 404, if true - tries to redirect to the above
        /// </summary>
        [ContentModelPropertyType("Container", "Must redirect (404 if false)", "MustRedirect")]
        public TrueOrFalse MustRedirect { get; set; }

        /// <summary>
        /// Url or page to redirect to if "MustRedirect"
        /// </summary>
        [ContentModelPropertyType("Container", "Redirect url or page (optional)", "RedirectUrlOrContent")]
        public UrlSelector RedirectUrlOrContent { get; set; }

        [JsonIgnore]
        public UrlSelectorConfigObject RedirectUrlOrContentConfigObject
        {
            get
            {
                return new UrlSelectorConfigObject
                {
                    AllowExternal = true,
                    AllowInternal = true,
                    AllowMailTo = false,
                    ContentPickerConfigObject = new ContentPickerConfigObject
                    {
                        AllowUnPublishedContent = false,
                        MaxPages = 1,
                        MinPages = 1
                    },
                    RequireDescription = false,
                    RequireName = false,
                    RequireLinkTitle = false,
                    LinkIsMandatory = this.MustRedirect != null ? this.MustRedirect.SavedValue : false
                };
            }
        }        
    }
}
